﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using LDSP.Util;

namespace LDSP.UI.EnterpriseAdmin
{
    public partial class SystemSetting : LDSP.BRL.BaseEnterpriseAdminPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    if (!IsPostBack)
                    {
                        this.InitDataSelect();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected bool RB()
        {
            return this.AllowEnterpriseUser();
        }

        protected void InitDataSelect()
        {
            //this.txt_implement_lable.Text = this.LoginUser.Enterprise.EnterpriseConfig.ActionFieldName;
            //this.txt_self_evaluation_lable.Text = this.LoginUser.Enterprise.EnterpriseConfig.SelfEvaluationFieldName;
            this.txt_CodificationActiveAlert.Text = this.LoginUser.Enterprise.EnterpriseConfig.CodificationActiveAlert > 0 ? this.LoginUser.Enterprise.EnterpriseConfig.CodificationActiveAlert.ToString() : string.Empty;
            this.txt_TaskPlanAlert.Text = this.LoginUser.Enterprise.EnterpriseConfig.TaskPlanAlertDays > 0 ? this.LoginUser.Enterprise.EnterpriseConfig.TaskPlanAlertDays.ToString() : string.Empty;

            this.BindDDLPageSize();
        }

        protected void BindDDLPageSize()
        {
            this.ddl_pageSize.Items.Clear();
            this.ddl_pageSize.Items.Add(new ListItem("10", "10"));
            this.ddl_pageSize.Items.Add(new ListItem("15", "15"));
            this.ddl_pageSize.Items.Add(new ListItem("20", "20"));

            try
            {
                this.ddl_pageSize.SelectedValue = this.LoginUser.Enterprise.EnterpriseConfig.LineQuantity.ToString();
            }
            catch
            {
                this.ddl_pageSize.SelectedIndex = 0;
            }
        }

        protected void ValidateDataBefore()
        {
        }

        protected void ValidateDataAfter()
        {

        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            //this.LoginUser.Enterprise.EnterpriseConfig.ActionFieldName = this.txt_implement_lable.Text;
            //this.LoginUser.Enterprise.EnterpriseConfig.SelfEvaluationFieldName = this.txt_self_evaluation_lable.Text;
            this.LoginUser.Enterprise.EnterpriseConfig.LineQuantity = this.GetInt(this.ddl_pageSize.SelectedValue);
            this.LoginUser.Enterprise.EnterpriseConfig.CodificationActiveAlert = this.GetInt(this.txt_CodificationActiveAlert.Text);
            this.LoginUser.Enterprise.EnterpriseConfig.TaskPlanAlertDays = this.GetInt(this.txt_TaskPlanAlert.Text);

            this.ValidateDataAfter();
        }

        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                this.SetData();
                SaveFavoriteCodificationTemplate(this.fup_FavoriteCodificationTemplate, TemplateType.适用法规);
                SaveFavoriteCodificationTemplate(this.fup_FavoriteStandardTemplate, TemplateType.适用标准);
                SaveFavoriteCodificationTemplate(this.fup_PlanTemplate, TemplateType.任务计划);

                //设置适用法规模板名
                if (this.fup_FavoriteCodificationTemplate.HasFile)
                {
                    this.LoginUser.Enterprise.EnterpriseConfig.FavoriteCodificationTemplateName = this.LoginUser.EnterpriseID + ".xls";
                }

                //设置适用法规标准名
                if (this.fup_FavoriteStandardTemplate.HasFile)
                {
                    this.LoginUser.Enterprise.EnterpriseConfig.FavoriteStandardTemplateName = this.LoginUser.EnterpriseID + ".xls";
                }

                //设置任务计划模板名
                if (this.fup_PlanTemplate.HasFile)
                {
                    this.LoginUser.Enterprise.EnterpriseConfig.TaskPlanTemplateName = this.LoginUser.EnterpriseID + ".xls";
                }

                this.JSAlert("更新成功！");
                this.InitDataSelect();
            }
            catch (TemplateValidateException ex)
            {
                this.JSAlert(ex.Message);
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void btn_reset_Click(object sender, EventArgs e)
        {
            if (sender == this.btnResetCodification)
            {
                this.LoginUser.Enterprise.EnterpriseConfig.FavoriteCodificationTemplateName = string.Empty;
            }
            else if (sender == this.btnResetStandard)
            {
                this.LoginUser.Enterprise.EnterpriseConfig.FavoriteStandardTemplateName = string.Empty;
            }
            else if (sender == this.btnResetPlan)
            {
                this.LoginUser.Enterprise.EnterpriseConfig.TaskPlanTemplateName = string.Empty;
            }

            this.JSAlert("模板重置成功！");
        }

        private void SaveFavoriteCodificationTemplate(FileUpload fup, TemplateType type)
        {
            if (fup.HasFile)
            {
                string fileName = fup.FileName;
                string folder = TemplateFactory.GetCustomTemplateFolderByType(type);
                string name = fileName.Substring(0, fileName.LastIndexOf('.'));
                string serverPath = System.Web.HttpContext.Current.Request.MapPath(folder + "/" + this.LoginUser.EnterpriseID + ".xls");
                fup.SaveAs(serverPath);

                //检验适用法规的列名是否符合规范。
                TemplateValidator validator = new TemplateValidator(Server);
                IList<string> invalidColumnNames = validator.ValidateFavoriteCodification(serverPath, type);

                if (invalidColumnNames.Count > 0)
                {
                    //显示出不符合规范的列名。
                    string invalidColumnStr = string.Empty;
                    foreach (string col in invalidColumnNames)
                    {
                        invalidColumnStr += col.Replace("\n", " ") + "\n";
                    }

                    throw new TemplateValidateException(type.ToString() + "模板列名不符合规范，请检查!\n"
                        + "如下:\n"
                        + invalidColumnStr);
                }
            }
        }
    }

}